<template>
  <div class="head">
    <div class="import" @click="dialogVisible = true">
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-daoru"></use>
      </svg>
      <p>导入</p>
    </div>
    <div class="export" @click="handleExport">
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-daochu"></use>
      </svg>
      <p>导出</p>
    </div>

  </div>

  <el-dialog v-model="dialogVisible" title="上传文件" width="500" :before-close="handleClose" class="upload">
    <!-- <span>引入文件组件待更改</span> -->
    <div class="select_file">
      <!-- casfile -->
      <div class="file">
        <!-- <p>cas文件：</p><span>请选择文件</span> -->
        <p>cas文件：</p>
        <el-upload ref="uploadCas" class="upload-demo"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15" :limit="1" :on-exceed="handleExceedCas"
          :auto-upload="false" accept=".cas" :on-change="handleFileChangeCas" :show-file-list="false">
          <!-- :show-file-list="false"  不显示已上传文件列表 -->

          <template #trigger>
            <!-- <p>cas文件：</p><span type="primary">请选择文件</span> -->
            <span>{{ casFilePath || '请选择文件' }}</span>
          </template>
        </el-upload>
      </div>
      <!-- datfile -->
      <div class="file datfile">
        <!-- <p>dat文件：</p><span>请选择文件</span> -->
        <p>dat文件：</p>
        <el-upload ref="uploadDat" class="upload-demo"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15" :limit="1" :on-exceed="handleExceedDat"
          :auto-upload="false" accept=".dat" :on-change="handleFileChangeDat" :show-file-list="false">
          <template #trigger>
            <span>{{ datFilePath || '请选择文件' }}</span>
          </template>
        </el-upload>
      </div>


    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          开始转换
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { saveAs } from 'file-saver';

import { ref } from 'vue';
export default {
  name: 'Head',
  data() {
    return {
      dialogVisible: false,
      uploadCas: ref(null),
      uploadDat: ref(null),
      casFilePath: '',
      datFilePath: '',
    };
  },
  methods: {
    handleClose(done) {
      // Simply call done() without showing the confirmation message box
      done();
    },
    //这里将选择的文件替换
    handleExceedCas(files) {
      // console.log(files);
      // 获取当前上传组件的实例
      const uploadInstance = this.$refs.uploadCas;
      // 清空当前已选择的文件列表
      uploadInstance.clearFiles();
      // 将新的文件添加到文件列表中
      uploadInstance.handleStart(files[files.length - 1]);
    },
    handleFileChangeCas(file, fileList) {
      // console.log(file);
      // console.log(file.name);
      this.casFilePath = file.name
      // console.log(fileList,'789');
      // if (file && file.status === 'ready') {
      //   this.casFilePath = file.name; // 获取文件名
      // } else {
      //   this.casFilePath = '';
      // }

    },
    //这里将选择的文件替换
    handleExceedDat(files) {
      // console.log(files,222);
      const uploadInstance = this.$refs.uploadDat;
      uploadInstance.clearFiles();
      uploadInstance.handleStart(files[files.length - 1]);
    },
    handleFileChangeDat(file, fileList) {
      // console.log(file,222);
      this.datFilePath = file.name
    },
    handleExport() {
      // 模拟生成 .dat 文件内容const content = "这是导出的 .dat 文件内容";
      const content = "这是导出的 .dat 文件内容";
      // 创建一个 Blob 对象，包含要导出的内容
      const blob = new Blob([content], { type: 'application/octet-stream' });
      // 使用 FileSaver.js 保存文件并提示用户选择保存位置
      saveAs(blob, 'exported.dat');

    }
  },
};
</script>



<style lang="scss" scoped>
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}

.head {

  margin-top: 6px;
  display: flex;

  .import {
    margin-left: 14px;
  }

  .export {
    margin-left: 24px;
  }

  .import:hover p,
  .export:hover p {
    color: #409EFF;
    /* 鼠标触碰时的颜色 */
  }

  .import:hover .icon,
  .export:hover .icon {
    fill: #409EFF;
    /* 鼠标触碰时图标颜色 */
  }

  p {
    text-align: center;
    color: #323133;
    font-size: 12px;
    // font-weight: 100;
    line-height: 20px;
    margin-top: 2px;
  }

  .icon {
    // padding: 4px;
    // width: 22px;
    // height: 22px;
    font-size: 20px;
  }


}

.upload {

  .select_file {
    padding: 20px 0;

    .file {
      margin-left: 20px;
      display: flex;

      p {
        font-weight: bold;
        color: initial;//初始颜色
        color: #606266;

      }
      span{
        color: initial;//初始颜色
        color: #606266;
        width: 370px;
      }
    }

    .datfile {
      margin-top: 12px;
    }
  }
}


</style>
